<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <search ref='search' :config='searchConfig' @search='search' @getHeight='getHeight' />

    <!-- table区域-begin -->
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        :border='true'
        :resizable='true'
        :showOverflow='true'
        size='mini'
        :loading='loading'
        :height='pageHeight-seachHeight-90'
        :align='left'
        :columns='columns'
        :data='dataSource'
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :toolbar-config='tableToolbar'
        :checkbox-config="checkboxConfig"
        v-bind='vexConfig'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #imgList='{ row }'>
          <viewer :images='row.imgList'>
            <a-avatar :src='row.imgList[0]' icon='user' shape='square' size='small'/>
          </viewer>
        </template>
        <template #toolbar_buttons>
          <div class='table-operator'>
            <a-button type="primary" @click="downExportXls('competingCollect_service')">导出</a-button>
          </div>
        </template>
        <template #detail="{ row,rowIndex }">
          <a @click="handleDetail(row)">{{ row.clientCode }}</a>
        </template>
        <template #status='{ row }'>
          <a-switch :checked='row.status' size='small' @change='handleStatus(row,$event)' />
        </template>
        <template #action='{ row:record }'>
          <a @click='handleEdit1(record)'>编辑</a>
          <span v-if='record.fileUrl'>
            <a-divider type='vertical' />
            <a :href='record.fileUrl'>下载</a>
          </span>
        </template>
        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync='ipagination.current'
            :page-size.sync='ipagination.pageSize'
            :total='ipagination.total'
            :pageSizes='ipagination.pageSizeOptions'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <sfaVisitLineEntity-modal ref="modalForm" @ok="modalFormOk"></sfaVisitLineEntity-modal>
    <!--    选择对应网点-->
    <JVisitClientModal
      modal-width="90%"
      ref="JVisitClientModal"
      @ok="handelSave">
    </JVisitClientModal>
    <!--    选择对线路组-->
    <JVisitGroupModal
      modal-width="90%"
      ref="JVisitGroupModal"
      @ok="handelSave">
    </JVisitGroupModal>
    <VisitConfigModal
      modal-width="90%"
      ref="VisitConfigModal"
      @ok="handelSaveOk">
    </VisitConfigModal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import SfaVisitLineEntityModal from './modules/SfaVisitLineModal'
import { PageListMixin } from '@/mixins/PageListMixin'
import moment from 'moment'
import JSelectUserPosition from '../../../components/cn-select-popup/JSelectUserPosition'
import { postAction } from '../../../api/manage'
import JVisitClientModal from './modules/JVisitClientModal'
import JVisitGroupModal from './modules/JVisitGroupModal'
import VisitConfigModal from '../../../components/cn-select-popup/VisitConfigModal'
import pick from 'lodash.pick'
export default {
  name: 'SfaVisitLineList',
  mixins: [PageListMixin],
  components: {
    VisitConfigModal,
    JSelectUserPosition,
    SfaVisitLineEntityModal,
    JVisitClientModal,
    JVisitGroupModal
  },
  data() {
    return {
      description: '竞品上报明细报表',
      operType: 0,
      //查询区域
      searchConfig:[
        {
          type: 'input',
          name: 'custCode',
          placeholder: '经销商编码'
        },
        {
          type: 'input',
          name: 'custName',
          placeholder: '经销商名称'
        },
        {
          type: 'input',
          name: 'competingCode',
          placeholder: '竞品编码'
        },
        {
          type: 'input',
          name: 'competingName',
          placeholder: '竞品名称'
        },
        {
          type: 'input',
          name: 'brand',
          placeholder: '所属品牌'
        },
        {
          type: 'date-range', //范围选择
          range: ['startTime', 'endTime'],
          name: 'range',
          format: 'YYYY-MM-DD',
          placeholder: ['开始时间', '结束时间']
        },
      ],
      // 表头
      columns: [
        { type: 'checkbox', width: 50, fixed: 'left' },
        {
          title: '经销商编码',
          minWidth:80,
          field: 'custCode'
        },
        {
          title: '经销商名称',
          minWidth:80,
          field: 'custName'
        },
        {
          title: '竞品编码',
          minWidth:80,
          field: 'competingCode'
        },
        {
          title: '竞品名称',
          minWidth:100,
          field: 'competingName'
        },
        {
          title: '所属品牌',
          minWidth:120,
          field: 'brand'
        },
        {
          title: '所属系列',
          minWidth:120,
          field: 'classifyName'
        },
        {
          title: '销售数量',
          minWidth:80,
          field: 'num'
        },
        {
          title: '是否有活动',
          minWidth:80,
          field: 'whetherActivityStr'
        },
        {
          title: '图片',
          minWidth:100,
          field: 'imgList',
          slots: { default: 'imgList' }
        },
        {
          title: '促销活动说明',
          minWidth:120,
          field: 'remarks'
        },
        {
          title: '提交人',
          minWidth:80,
          field: 'createName'
        },
        {
          title: '提交人账号',
          minWidth:100,
          field: 'createBy'
        },
        {
          title: '提交人组织',
          minWidth:100,
          field: 'orgName'
        },
        {
          title: '提交时间',
          minWidth:100,
          field: 'createTime'
        },
        // {
        //   title: '操作',
        //   field: 'action',
        //   fixed: 'right',
        //   minWidth:80,
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      isorter: {
        permflag: 'competing_info_list'
      },
      url: {
        list: window._CONFIG['domianSfaURL'] + '/competing/collect/findPageList',
        delete: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/delete',
        detail: window._CONFIG['domianURL'] + '/cnUserController/getUserByIdOrCode',
        deleteBatch: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/deleteBatch',
        exportXlsUrl: window._CONFIG['domianSfaURL'] + 'visit/sfaVisitLineController/exportXls',
        importExcelUrl: window._CONFIG['domianSfaURL'] + 'visit/sfaVisitLineController/importExcel'
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },

  methods: {
    //限制当天之前的日期不可选
    disabledDate(current) {
      // return current && current <moment().subtract(1, "days"); //当天之前的不可选，不包括当天
      return current && current < moment().endOf('day') //当天之前的不可选，包括当天
    },
    searchQuery() {
      if (this.checkParam(false)) {
        this.loadData(1)
      }
    },
    handleAddLineByGroup() {
      if (this.checkParam(true)) {
        this.operType = 2
        this.$refs.JVisitGroupModal.showModal(this.queryParam.visitPosCode)
      }

    },
    handleAddLine() {
      if (this.checkParam(true)) {
        this.operType = 1
        this.$refs.JVisitClientModal.showModal(this.queryParam.visitPosCode)
      }

    },
    checkParam(ischeck) {
      if (ischeck) {
        if (!this.queryParam.visitPosCode) {
          this.$message.warning('请选择拜访人')
          return false
        }
        if (!this.queryParam.visitDates || this.queryParam.visitDates.length == 0) {
          this.$message.warning('请选择拜访时间')
          return false
        }
        let temp = []
        this.queryParam.visitDates.map(row => {
          temp.push(moment(row).format('YYYY-MM-DD'))
        })
        this.queryParam.visitDates = temp
      } else {
        this.queryParam.tempVisitDate = moment(this.queryParam.tempVisitDate).format('YYYY-MM-DD')
      }
      return true
    },
    handelSave(clients, groupCode) {
      const record = {
        visitPosCode: this.queryParam.visitPosCode,
        visitDates: this.queryParam.visitDates,
        operType: this.operType
      }
      if (this.operType === 1) {
        record.lineVos = clients
      } else {
        record.groups = clients
      }
      var that = this
      postAction(window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/saveOrUpdate', record).then(async (res) => {
        if (res.success) {
          that.$message.success(res.message)
          await this.loadData()
          if (this.operType === 1) {
            this.$refs.JVisitClientModal.close()
          } else {
            this.$refs.JVisitGroupModal.close()
          }
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        if (this.operType === 1) {
          this.$refs.JVisitClientModal.confirmLoading = false
        } else {
          this.$refs.JVisitGroupModal.confirmLoading = false
        }
      })
    },
    async handleClientModal(record) {
      if (this.selectedRowKeys.length != 1) {
        this.$message.warning('请选择一条记录！')
        return
      }
      this.$refs.JSelectClientModal.showModal(this.selectionRows[0].groupCode)
    },
    handleAddUser() {
      this.$refs.VisitConfigModal.showModal()
    },
    handelSaveOk(pos){
      if (pos!=null && pos !=null){
        this.$set(this.queryParam,"visitPosCode",pos.positionCode);
        this.$set(this.queryParam,"queryVisitPosName",pos.positionName+"-"+pos.realname);
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>